var app = require('express')

var headerfix = require('../plugins/headerFix')

var sqlmodule = require('../plugins/sql')//全局共享的实例

var statusobj = require('../plugins/routerStatus')

var sqlinits = sqlmodule.sqlinit

var routers = app.Router()

// routers

headerfix(routers)

routers.post('/sqladd', (req, res) => {
    sqlinits(res, (conn) => {
        if (!req.body.name && !req.body.birth) {
            res.send({
                msg: '请至少传一个数据进来',
                status: statusobj.fail
            })
            return;
        }
        //req.body
        //res.send
        var linkend = (type, result) => {
            type == -1 ?
                res.send({
                    msg: 'fail',
                    status: statusobj.fail,
                    data: result
                }) :
                res.send({
                    msg: 'ok',
                    status: statusobj.ok,
                    data: result
                })
        }
        //数据库里面的Date类型对应这种${new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + (new Date().getDate())}
        conn.query(`INSERT INTO list (name,birth,updateDate) VALUES ('${req.body.name || 'null'}','${req.body.birth || 'null'}','${new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + (new Date().getDate())}')`, (err, result) => {
            conn.release();
            if (err) {
                linkend(-1, err)
            } else {
                linkend(1, result)
            }
        })
    })
})

routers.post('/sqldel', (req, res) => {
    sqlinits(res, (conn) => {
        if (!req.body.hasOwnProperty('id')) {
            res.send({
                msg: '没有id传进来',
                status: statusobj.fail
            })
            return;
        }
        //req.body
        //res.send
        var linkend = (type, result) => {
            type == -1 ?
                res.send({
                    msg: 'fail',
                    status: statusobj.fail
                }) :
                res.send({
                    msg: 'ok',
                    status: statusobj.ok
                })
        }
        conn.query(`DELETE FROM list WHERE id='${req.body.id}'`, (err, result) => {
            conn.release();
            if (err) {
                linkend(-1, err)
            } else {
                linkend(1, result)
            }
        })
    })
})

routers.post('/sqlupdate', (req, res) => {
    sqlinits(res, (conn) => {
        if (!req.body.hasOwnProperty('id')) {
            res.send({
                msg: '没有id传进来',
                status: statusobj.fail
            })
            return;
        }
        if (!req.body.name && !req.body.birth) {
            res.send({
                msg: '请至少传一个数据进来',
                status: statusobj.fail
            })
            return;
        }
        //合并参数操作
        var arr = []
        var objs = {
            name: req.body.name,
            birth: req.body.birth
        }
        for (var i in objs) {
            if (objs.hasOwnProperty(i) && objs[i] != null && objs[i] != undefined && objs[i] != '') {
                arr.push(i + '=\'' + objs[i] + '\'')
            }
        }
        var lastobj = arr.join(',')
        //req.body
        //res.send
        var linkend = (type, result) => {
            type == -1 ?
                res.send({
                    msg: 'fail',
                    status: statusobj.fail,
                    data: result
                }) :
                res.send({
                    msg: 'ok',
                    status: statusobj.ok
                })
        }
        conn.query(`UPDATE list SET `
            + lastobj
            + ` WHERE id='${req.body.id}'`, (err, result) => {
                conn.release();
                if (err) {
                    linkend(-1, err)
                } else {
                    linkend(1, result)
                }
            })
    })
})

//模拟验证码处理

routers.get('/mock/mobile', (req, res) => {
    if (routers.custom && routers.custom.num && routers.custom.num > 100000) {
        res.send({
            msg: '系统繁忙，请耐心等候',
            status: 200
        })
        return;
    }
    var mobile = req.query.mobile || '18929974772'//假设拿到号码
    //假设通过运营商拿到验证码
    var fns = () => {
        if (routers.custom.mob['m_' + mobile]) {
            routers.custom.mob['m_' + mobile].code = ''
        }
        setTimeout(() => {
            routers.custom.mob['m_' + mobile] = {
                code: '112233',//假设运营商给到验证码
                inters: function () {
                    var times = 0
                    var flags = setInterval(() => {
                        if (routers.custom.mob['m_' + mobile].code == '') {
                            clearInterval(flags)
                        }
                        if (times < 120) {
                            times++
                        } else {
                            clearInterval(flags)
                            delete routers.custom.mob['m_' + mobile]
                            routers.custom.num--
                        }
                    }, 1000)
                }
            }
            routers.custom.mob['m_' + mobile].inters()
        }, 1100)
    }
    const hadgetcode = () => {
        if (!routers.custom) {
            routers.custom = {}
            routers.custom.mob = {}
            fns()
        } else {
            if (routers.custom.mob['m_' + mobile]) { } else {
                routers.custom.num = (routers.custom.num || 0) + 1
            }
            fns()
        }
        //
        res.send({
            msg: '获取验证码成功'
        })
    }
    hadgetcode()
})

routers.get('/mock/mobilelogin', (req, res) => {
    var mobile = '18929974772'//假设拿到号码,信息正确，登录成功
    if (routers.custom.mob['m_' + mobile]) {
        console.log(routers.custom.num)
        if (req.query.code == routers.custom.mob['m_' + mobile].code) {
            routers.custom.mob['m_' + mobile].code = ''
            setTimeout(() => {
                delete routers.custom.mob['m_' + mobile]
                routers.custom.num--
                res.send({
                    msg: '绑定成功',
                    status: 200
                })
            }, 1100)
        } else {
            routers.custom.mob['m_' + mobile].code = ''
            setTimeout(() => {
                delete routers.custom.mob['m_' + mobile]
                routers.custom.num--
                res.send({
                    msg: '验证码错误',
                    status: 200
                })
            }, 1100)
        }
    } else {
        res.send({
            msg: '验证码错误',
            status: 200
        })
    }
})

module.exports = routers